package com.client.rule.triple;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gwt.user.client.Window;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.shared.rule.TypeComplexTriple;
import com.shared.rule.TypeDataRule;

public class UiComplexTriple extends UiTriple{
	
	List<UiTriple> subTriple = new ArrayList<UiTriple>();
	private TypeComplexTriple typeComplexTriple;
	private String label;
	
	public UiComplexTriple() {
		super();
		
	}
	
	public UiComplexTriple(String label){
		super();
		this.label = label;
	}
	
	public UiComplexTriple(String label,TypeComplexTriple typeComplexTriple){
		super();
		this.label = label;
		this.typeComplexTriple =typeComplexTriple;
	}
	
	/**
	 * @return the subTriple
	 */
	public List<UiTriple> getSubTriple() {
		return subTriple;
	}

	/**
	 * @param subTriple the subTriple to set
	 */
	public void setSubTriple(List<UiTriple> subTriple) {
		this.subTriple = subTriple;
	}



	
	
	
		
	
		

	@Override
	public void generateFromXML(Node xmlNode) {
		Element tripleElement = (Element) xmlNode;
		//Window.alert(tripleElement.toString());
		label = tripleElement.getAttribute("type");
		//Window.alert(tripleElement.getAttribute("type"));
		typeComplexTriple = TypeComplexTriple.getType(tripleElement.getAttribute("type"));
		//Window.alert(label);
		for (Node n = xmlNode.getFirstChild(); n != null; n = n.getNextSibling())	{				
			UiTriple triple;
			Element rElement = (Element) n;
			//Window.alert("ComplexTriple "+rElement.toString());
			//Window.alert("ComplexTriple "+rElement.getAttribute("type"));
			if(!TypeComplexTriple.isComplex(rElement.getAttribute("type"))){	
				//Window.alert("ComplexTriple Simple");
				triple = new UiSimpleTriple();					
			}else{
				//Window.alert("ComplexTriple Complex");
				triple = new UiComplexTriple();					
			}
			triple.generateFromXML(n);
			subTriple.add(triple);
		}
		
	}
	
	public void add(UiTriple triple){
		subTriple.add(triple);
	}

	@Override
	public boolean isComplex() {		
		return true;
	}

	

	@Override
	public String toXml(Map<String,TypeDataRule> mapData) {		
		String xml="<Triple type=\""+typeComplexTriple.toString()+"\">";		
		for(UiTriple triple : subTriple){
			xml=xml+triple.toXml(mapData);
		}		
		xml=xml+"</Triple>";		
		return xml;
	}



	/**
	 * @param label the label to set
	 */
	public void setLabel(String label) {
		this.label = label;
	}



	/**
	 * @return the label
	 */
	public String getLabel() {
		return label;
	}

	@Override
	public void getUsedDataXml(Map<String, String> usedData,Map<String,TypeDataRule> mapData) {
		for(UiTriple triple : subTriple) triple.getUsedDataXml(usedData,mapData);		
	}



	
	
}
